package com.wisdytech.linkdcs.partLife.service.impl;

import com.wisdytech.linkdcs.partLife.model.KeyPart;
import com.wisdytech.linkdcs.partLife.model.Part;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.github.pagehelper.PageInfo;
import com.wisdytech.common.bean.Filter;
import com.wisdytech.common.constants.CommonConstants;
import com.wisdytech.common.exception.ServiceException;
import com.wisdytech.common.handler.BaseService;
import com.wisdytech.common.utils.BaseUtil;
import com.wisdytech.linkdcs.partLife.dao.IKeyPartDao;
import com.wisdytech.linkdcs.partLife.dao.IPartDao;
import com.wisdytech.linkdcs.partLife.service.IKeyPartService;
import com.wisdytech.linkdcs.partLife.vo.KeyPartVO;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  关键备件服务实现类
 * </p>
 *
 * @author chenbin
 * @since 2018-06-05
 */
@Service("iKeyPartService")
@Transactional
public class KeyPartServiceImpl extends BaseService<IKeyPartDao, KeyPart> implements IKeyPartService {
    /**
     * 注入关键备件配置dao
     */
	@Resource
	private IKeyPartDao iKeyPartDao;
    /**
     * 注入备件信息管理dao
     */
	@Resource
	private IPartDao iPartDao;
	
	@Override
	public PageInfo<KeyPart> listKeyPartByFilter(String partCode, String partName, String oSerialNum, String status,
			Filter filter) {
        Map<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("partCode", partCode);
        queryMap.put("partName", partName);
        queryMap.put("oSerialNum", oSerialNum);
        queryMap.put("status", status);
        List<KeyPart> lists =
                super.beforeList(IKeyPartDao.class, filter).listKeyPartByFilter(queryMap);
        PageInfo<KeyPart> keyPartPage=new PageInfo<>(lists);
        return keyPartPage;
	}

	@Override
	public void addKeyPart(KeyPart keyPart, String loginName) {
		EntityWrapper<KeyPart> ew = new EntityWrapper<>();
        //验证备件序列号的唯一性
        List<KeyPart> partList = new ArrayList<>();
        partList = iKeyPartDao.selectPage(new Page<Part>(1, 1), ew.eq("O_SERIAL_NUM", keyPart.getoSerialNum()).eq("del_flag", CommonConstants.DEL_FLAG));
        if (!partList.isEmpty()) {
        	throw new ServiceException("该备件序列号被使用");
        }
		keyPart.setPkId(BaseUtil.getUUID());
		keyPart.setDelFlag(CommonConstants.DEL_FLAG);
		keyPart.setCreateUser(loginName);
		keyPart.setCreateDate(new Date());
        int partCount = iKeyPartDao.insert(keyPart);
        if (partCount <= 0) {
            throw new ServiceException("新增失败");
        }
		
	}

	@Override
	public KeyPartVO getKeyPartById(String pkId) {
		if(pkId.isEmpty()) {
		  return null;
		}
		KeyPart keyPart = iKeyPartDao.selectById(pkId);
		Part part = new Part();
		part.setPkId(keyPart.getPartId());
		Part partO = iPartDao.selectOne(part);
		KeyPartVO keyPartVO = new KeyPartVO();
		keyPartVO.setPkId(keyPart.getPkId());
		keyPartVO.setPartId(keyPart.getPartId());
		keyPartVO.setStatus(keyPart.getStatus());
		keyPartVO.setPartCode(partO.getPartCode());
		keyPartVO.setPartName(partO.getPartName());
		keyPartVO.setoSerialNum(keyPart.getoSerialNum());
		keyPartVO.setStandardLife(keyPart.getStandardLife());
		return keyPartVO;
	}

	@Override
	public void updateKeyPart(KeyPart keyPart, String loginName) {
		EntityWrapper<KeyPart> ew = new EntityWrapper<>();;
        //验证备件编码的唯一性--修改功能
		List<KeyPart> partList;
		partList = iKeyPartDao.selectPage(new Page<Part>(1, 1),ew.eq("O_SERIAL_NUM", keyPart.getoSerialNum()).ne("pk_id", keyPart.getPkId()));
        if (partList.size() > 0){
        	throw new ServiceException("该备件编码被使用");
        }
		keyPart.setUpdateUser(loginName);
		keyPart.setUpdateDate(new Date());
        int partCount = iKeyPartDao.updateById(keyPart);
        if (partCount <= 0) {
            throw new ServiceException("修改失败");
        }
		
	}

	@Override
	public void delKeyPartById(String ids) {
        List<String> idList = new ArrayList<String>();
        String[] id = ids.split(",");
        for (int i = 0; i < id.length; i++) {
            idList.add(id[i]);
        }
        for(String pkId : id) {
        	KeyPart keyPart = iKeyPartDao.selectById(pkId);
        	if(keyPart.getStatus()==2) {
        		throw new ServiceException("记录中存在已被引用关键备件");
        	}
        }
        int partCount = iKeyPartDao.deleteBatchIds(idList);
        if (partCount <= 0) {
            throw new ServiceException("删除失败");
        }
		
	}

	@Override
	public List<KeyPart> getAllKeyPartByPartCode(String search, String partCode) {
        Map<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("search", search);
        queryMap.put("partCode", partCode);
		return iKeyPartDao.getAllKeyPartByPartCode(queryMap);
	}

	@Override
	public List<KeyPart> searchKeyPartByoSerial(String oSerialNum, String partName,Filter filter) {
        Map<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("oSerialNum", oSerialNum);
        queryMap.put("partName", partName);
        List<KeyPart> lists =
                super.beforeList(IKeyPartDao.class, filter).searchKeyPartByoSerial(queryMap);
		return lists;
	}

	@Override
	public List<KeyPart> getAllKeyPartByPartId(String search, String partId) {
		EntityWrapper<KeyPart> ew = new EntityWrapper<>();
		ew.like("O_SERIAL_NUM", search).eq("PART_ID", partId).eq("STATUS",0.0 );
		return iKeyPartDao.selectList(ew);
	}

	@Override
	public KeyPart getKeyPartByOseri(String oSerialNum) {
		KeyPart part = new KeyPart();
		part.setoSerialNum(oSerialNum);
		return iKeyPartDao.selectOne(part);
		
	}

	@Override
	public KeyPart getKeyPartByPartId(String pkId) {
		if(pkId.isEmpty()) {
			return null;
		}
		KeyPart keyPart = new KeyPart();
		keyPart.setPartId(pkId);
		keyPart.setStatus(2.0);
		KeyPart keyPartReal = iKeyPartDao.selectOne(keyPart);
		return keyPartReal;
	}
	
	

}
